/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package name_service;

import java.io.IOException;
import mware_lib.Communicator.Connection;
import mware_lib.Communicator.Server;
import mware_lib.NameService;
import mware_lib.RequestThread;

/**
 * @author Gortex
 */
public class NameServiceRunner {

    public static void main(String[] args) {

        int nameServicePort = getPort(args);
        if (nameServicePort > 1024) {
            NameService ns = new NameServiceImpl();
            NameServiceSkeleton nsSkeleton = new NameServiceSkeleton(ns);

            try {
                Server server = new Server(nameServicePort);

                while (true) {
                    Connection connection = server.getConnection();
                    RequestThread thread = new RequestThread(connection, nsSkeleton);
                    thread.start();
                }
            } catch (IOException ex) {
            }
        } else {
            System.err.println("Ungueltiger Port. Bitte anderen Port waehlen.");
        }
    }

    
    private static int getPort(String[] args) {
        if (args.length == 1) {
            try {
                return Integer.parseInt(args[0]);
            } catch (NumberFormatException ex) {
                return -1;
            }
        } else {
            return -1;
        }
    }
    
}
